from django.db import models
from users.models import User,Addr
from goods.models import Good

# Create your models here.
# 订单表
# 订单id、  varchar(100)，主键
# 用户、 外键
# 收货地址、 外键
# 总价、decimal(9,2)
# 总商品数、int
# 运费、decimal(7, 2)
# 支付方式、char(10)  （1支付宝  2银联）
# 支付状态、char(10)
# 创建时间、 datetime
# 更新时间， datetime
# &nbsp;


class Order(models.Model):
    STATUS_CHOICE = (
        ("0","待支付"),
        ("1","待发货"),
        ("2","待收货"),
        ("3","待评价"),
        ("4","已完成"),
    )
    order_id = models.CharField("订单编号", max_length=100, primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    addr = models.ForeignKey(Addr, on_delete=models.CASCADE, verbose_name='收获地址')
    total_amount = models.DecimalField("总价格", max_digits=9, decimal_places=2)
    total_count = models.IntegerField("总商品数", default=1)
    # 运费
    freight = models.DecimalField("运费", max_digits=7, decimal_places=2)

    # 支付方式
    pay_method = models.CharField("支付方式", max_length=10, choices=(("1","支付宝"),("2","银联")), default="1")
    # 支付状态
    pay_status = models.CharField("支付状态", max_length=10,choices=STATUS_CHOICE)

    # 创建时间
    created_time = models.DateTimeField("创建时间", auto_now_add=True)

    # 更新时间
    updated_time = models.DateTimeField("更新时间", auto_now=True)

    def __str__(self):
        return "订单:%s"%self.order_id

    class Meta:
        db_table = 'order_tb'
        verbose_name_plural = "订单表"


# ==订单商品表==
# id、int   主键
# 商品id、外键
# 数量、 int
# 单价、decimal(7,2)
# 评价、text
# 评分、smallint, default =5    （0-5）
# 订单id、 外键
# 是否匿名评价、boolean
# 是否评价 boolean

class OrderGood(models.Model):
    SCORE_CHOICE = (
        (0,"0分"),
        (1,"20分"),
        (2,"40分"),
        (3,"60分"),
        (4,"80分"),
        (5,"100分")
    )
    good = models.ForeignKey(Good, on_delete=models.CASCADE, verbose_name="商品")
    count = models.IntegerField("购买数量", default=1)
    price = models.DecimalField("商品单价", max_digits=7, decimal_places=2)
    # 商品的评价
    comment = models.TextField("商品评价", default="")
    score = models.SmallIntegerField("商品评分", default=5, choices=SCORE_CHOICE)

    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name="所属订单")

    is_anonymous = models.BooleanField("是否匿名评价", default=False)
    is_comment = models.BooleanField("是否评价", default=False)

    def __str__(self):
        return "订单%s中的商品%s" % (self.order.order_id, self.good.sku_name)

    class Meta:
        db_table = "order_good_tb"
        verbose_name_plural = "订单商品表"










